using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;

namespace DataAccess
{
	public class NewsData
	{
		#region Fields
		protected static SqlConnection dbConn = new SqlConnection(Config.Global.Settings.ConnectionString);
		#endregion

		#region Get Object Method
		private static object getNullObject(object obj)
		{
			return obj == null ? DBNull.Value : obj;
		}
		private static object getNullValue(object obj)
		{
			return obj == DBNull.Value ? null : obj;
		}
		#endregion

		#region Constructors
		public NewsData() {}
		#endregion

		#region Methods

		/// <summary>
		/// Saves a record to the News table.
		/// </summary>
		/// <param name="newsInfo"></param>
		/// <returns>ID of Saved record</returns>
		public static int? Insert(NewsInfo newsInfo)
		{
			SqlCommand dbCmd = new SqlCommand("[uspNewsInsert]", dbConn);
			dbCmd.CommandType = CommandType.StoredProcedure;

			dbCmd.Parameters.AddWithValue("@SiteID", getNullObject(newsInfo.SiteID));
			dbCmd.Parameters.AddWithValue("@NewsTitle", getNullObject(newsInfo.NewsTitle));
			dbCmd.Parameters.AddWithValue("@NewsImage", getNullObject(newsInfo.NewsImage));
			dbCmd.Parameters.AddWithValue("@NewsDescription", getNullObject(newsInfo.NewsDescription));
			dbCmd.Parameters.AddWithValue("@NewsContent", getNullObject(newsInfo.NewsContent));
			dbCmd.Parameters.AddWithValue("@DatePosted", getNullObject(newsInfo.DatePosted));
			dbCmd.Parameters.AddWithValue("@NewsStatus", getNullObject(newsInfo.NewsStatus));
			dbCmd.Parameters.AddWithValue("@RETURN_VALUE", SqlDbType.Int).Direction = ParameterDirection.ReturnValue;

			try
			{
				dbConn.Open();
				dbCmd.ExecuteNonQuery();
				return Convert.ToInt32(dbCmd.Parameters["@RETURN_VALUE"].Value);
			}
			catch (Exception)
			{
				return null;
			}
			finally
			{
				dbConn.Close();
			}
		}

		/// <summary>
		/// Updates a record in the News table.
		/// </summary>
		/// <param name="newsInfo"></param>
		/// <returns>true: successful; false: unsuccessful</returns>
		public static bool Update(NewsInfo newsInfo)
		{
			SqlCommand dbCmd = new SqlCommand("[uspNewsUpdate]", dbConn);
			dbCmd.CommandType = CommandType.StoredProcedure;

			dbCmd.Parameters.AddWithValue("@NewsID", getNullObject(newsInfo.NewsID));
			dbCmd.Parameters.AddWithValue("@SiteID", getNullObject(newsInfo.SiteID));
			dbCmd.Parameters.AddWithValue("@NewsTitle", getNullObject(newsInfo.NewsTitle));
			dbCmd.Parameters.AddWithValue("@NewsImage", getNullObject(newsInfo.NewsImage));
			dbCmd.Parameters.AddWithValue("@NewsDescription", getNullObject(newsInfo.NewsDescription));
			dbCmd.Parameters.AddWithValue("@NewsContent", getNullObject(newsInfo.NewsContent));
			dbCmd.Parameters.AddWithValue("@DatePosted", getNullObject(newsInfo.DatePosted));
			dbCmd.Parameters.AddWithValue("@NewsStatus", getNullObject(newsInfo.NewsStatus));
			dbCmd.Parameters.AddWithValue("@RETURN_VALUE", SqlDbType.Int).Direction = ParameterDirection.ReturnValue;

			try
			{
				dbConn.Open();
				dbCmd.ExecuteNonQuery();
				return Convert.ToInt32(dbCmd.Parameters["@RETURN_VALUE"].Value)>0;
			}
			catch (Exception)
			{
				return false;
			}
			finally
			{
				dbConn.Close();
			}
		}

		/// <summary>
		/// Deletes a record from the News table by its primary key.
		/// </summary>
		/// <param name="newsID"></param>
		/// <returns>true: successful; false: unsuccessful</returns>
		public static bool DeleteByNewsID(int newsID)
		{
			SqlCommand dbCmd = new SqlCommand("[uspNewsDeleteByNewsID]", dbConn);
			dbCmd.CommandType = CommandType.StoredProcedure;

			dbCmd.Parameters.AddWithValue("@NewsID", newsID);
			dbCmd.Parameters.AddWithValue("@RETURN_VALUE", SqlDbType.Int).Direction = ParameterDirection.ReturnValue;

			try
			{
				dbConn.Open();
				dbCmd.ExecuteNonQuery();
				return Convert.ToInt32(dbCmd.Parameters["@RETURN_VALUE"].Value)>0;
			}
			catch (Exception)
			{
				return false;
			}
			finally
			{
				dbConn.Close();
			}
		}

		/// <summary>
		/// Deletes a record from the News table by a foreign key.
		/// </summary>
		/// <param name="siteID"></param>
		/// <returns>true: successful; false: unsuccessful</returns>
		public static bool DeleteAllBySiteID(int siteID)
		{
			SqlCommand dbCmd = new SqlCommand("[uspNewsDeleteAllBySiteID]", dbConn);
			dbCmd.CommandType = CommandType.StoredProcedure;

			dbCmd.Parameters.AddWithValue("@SiteID", siteID);
			dbCmd.Parameters.AddWithValue("@RETURN_VALUE", SqlDbType.Int).Direction = ParameterDirection.ReturnValue;

			try
			{
				dbConn.Open();
				dbCmd.ExecuteNonQuery();
				return Convert.ToInt32(dbCmd.Parameters["@RETURN_VALUE"].Value)>0;
			}
			catch (Exception)
			{
				return false;
			}
			finally
			{
				dbConn.Close();
			}
		}

		/// <summary>
		/// Selects a single record from the News table.
		/// </summary>
		/// <param name="newsID"></param>
		/// <returns>NewsInfo: A record from the News table by its primary key.</returns>
		public static NewsInfo SelectByNewsID(int newsID)
		{
			SqlCommand dbCmd = new SqlCommand("[uspNewsSelectByNewsID]", dbConn);
			dbCmd.CommandType = CommandType.StoredProcedure;
			NewsInfo newsInfo =  new NewsInfo();

			dbCmd.Parameters.AddWithValue("@NewsID", newsID);

			try
			{
				dbConn.Open();
				SqlDataReader dataReader = dbCmd.ExecuteReader();

				if (dataReader.Read())
				{
					newsInfo.NewsID = System.Convert.ToInt32(getNullValue(dataReader["NewsID"]));
					newsInfo.SiteID = System.Convert.ToInt32(getNullValue(dataReader["SiteID"]));
					newsInfo.NewsTitle = System.Convert.ToString(getNullValue(dataReader["NewsTitle"]));
					newsInfo.NewsImage = System.Convert.ToString(getNullValue(dataReader["NewsImage"]));
					newsInfo.NewsDescription = System.Convert.ToString(getNullValue(dataReader["NewsDescription"]));
					newsInfo.NewsContent = System.Convert.ToString(getNullValue(dataReader["NewsContent"]));
					newsInfo.DatePosted = System.Convert.ToDateTime(getNullValue(dataReader["DatePosted"]));
					newsInfo.NewsStatus = System.Convert.ToBoolean(getNullValue(dataReader["NewsStatus"]));
				}

				dataReader.Close();
				return newsInfo;
			}
			catch (Exception)
			{
				return new NewsInfo();
			}
			finally
			{
				dbConn.Close();
			}
		}

		/// <summary>
		/// Selects all records from the News table.
		/// </summary>
		/// <return>DataTable</return>
		public static DataTable SelectAll()
		{
			SqlCommand dbCmd = new SqlCommand("[uspNewsSelectAll]",dbConn);
			dbCmd.CommandType = CommandType.StoredProcedure;
			DataTable dtbNews =  new DataTable();

			try
			{
				dbConn.Open();
				SqlDataReader dataReader = dbCmd.ExecuteReader();

				if (dataReader.HasRows)
				{
					dtbNews.Load(dataReader);
				}

				dataReader.Close();
				return dtbNews;
			}
			catch (Exception)
			{
				return null;
			}
			finally
			{
				dbConn.Close();
			}
		}

		/// <summary>
		/// Selects all records from the News table by a foreign key.
		/// </summary>
		/// <param name="siteID"></param>
		/// <return>All records from the News table by a foreign key.</return>
		public static DataTable SelectAllBySiteID(int siteID)
		{
			SqlCommand dbCmd = new SqlCommand("[uspNewsSelectAllBySiteID]", dbConn);
			dbCmd.CommandType = CommandType.StoredProcedure;
			DataTable dtbNews =  new DataTable();

			dbCmd.Parameters.AddWithValue("@SiteID", siteID);

			try
			{
				dbConn.Open();
				SqlDataReader dataReader = dbCmd.ExecuteReader();

				if (dataReader.HasRows)
				{
					dtbNews.Load(dataReader);
				}

				dataReader.Close();
				return dtbNews;
			}
			catch (Exception)
			{
				return null;
			}
			finally
			{
				dbConn.Close();
			}
		}

		#endregion
	}
}
